package edu.sandau.b230111.b23011125.Controller;

import edu.sandau.b230111.b23011125.Global.GlobalDefine;
import edu.sandau.b230111.b23011125.Global.ProblemWithHistory;
import edu.sandau.b230111.b23011125.Model.History;
import edu.sandau.b230111.b23011125.Model.Problem;
import edu.sandau.b230111.b23011125.Service.HistoryService;
import edu.sandau.b230111.b23011125.Service.ProblemService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
public class ProblemController {
    @Autowired
    private ProblemService problemService;
    @Autowired
    private HistoryService historyService;

    @RequestMapping("/problem/list")
    public String listAllProblem(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "0") String pageCnt, ModelMap map){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        int pageIndex = Integer.parseInt(pageCnt);
        Page<Problem> problems = problemService.queryAllProblem(pageIndex, GlobalDefine.PAGESIZE);
        map.put("totalElement",problems.getTotalElements());
        map.put("totalPage",problems.getTotalPages());
        map.put("nowPage",problems.getNumber());
        List<Problem> problemContent = problems.getContent();

        List<ProblemWithHistory> problemWithHistories = new ArrayList<>();
        for(Problem x : problemContent){
            ProblemWithHistory y = new ProblemWithHistory();
            y.setProblem(x);
            History his = historyService.queryHistory(loggedUserid,x.getProblemid());
            y.setHistory(his);
            problemWithHistories.add(y);
        }
        map.put("problems",problemWithHistories);
        String isAdmin = (String)request.getSession().getAttribute("isAdmin");
        if(isAdmin==null){
            map.put("isAdmin","NO");
        }
        else{
            map.put("isAdmin","YES");
        }

        return "problem_list";
    }

    @RequestMapping("/problem/{problemid}")
    public String problemDetails(HttpServletRequest request,ModelMap map,@PathVariable("problemid") String problemid){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        Problem problem = problemService.queryProblemById(problemid);
        if(problem==null){ //题号不存在
           return "notfound";
        }
        String isAdmin = (String)request.getSession().getAttribute("isAdmin");
        if(isAdmin==null){
            map.put("isAdmin","NO");
        }
        else{
            map.put("isAdmin","YES");
        }
        map.put("problem",problem);
        boolean iscontest = false;
        map.put("iscontest",iscontest);
        return "problem";
    }

    @RequestMapping("/problem/modify/{problemid}")
    public String modifyProblem(HttpServletRequest request,ModelMap map,@PathVariable("problemid") String problemid){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        String isadmin = (String)request.getSession().getAttribute("isAdmin");
        if(null==isadmin){//非管理员
            request.getSession().removeAttribute("userid");
            request.getSession().removeAttribute("isAdmin");
            return "redirect:login";
        }
        Problem problem = problemService.queryProblemById(problemid);
        if(problem==null){ //题号不存在
            return "notfound";
        }
        map.put("problem",problem);
        return "problem_modify";
    }

    @RequestMapping("/problem/addproblem")
    public String addProblem(HttpServletRequest request,ModelMap map){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        String isadmin = (String)request.getSession().getAttribute("isAdmin");
        if(null==isadmin){//非管理员
            request.getSession().removeAttribute("userid");
            request.getSession().removeAttribute("isAdmin");
            return "redirect:login";
        }

        return "problem_add";
    }

    @RequestMapping("/api/problem/save")
    public String saveProblem(Problem problem){
        if(null==problem.getProblemid()){
            problem.setProblemid(UUID.randomUUID().toString());
        }
        problemService.addProblem(problem);
        return "redirect:/problem/list";
    }

    @RequestMapping("api/problem/delete/{problemid}")
    public String deleteProblem(HttpServletRequest request,@PathVariable("problemid") String problemid){
        String loggedUserid = (String)request.getSession().getAttribute("userid");
        if(null==loggedUserid){//未登录
            return "redirect:/login";
        }
        String isadmin = (String)request.getSession().getAttribute("isAdmin");
        if(null==isadmin){//非管理员
            request.getSession().removeAttribute("userid");
            request.getSession().removeAttribute("isAdmin");
            return "redirect:login";
        }
        problemService.deleteProblem(problemid);
        return "redirect:/problem/list";
    }
}

